#ifndef YEYAGANGCONTROL_H
#define YEYAGANGCONTROL_H

#include <QWidget>
#include <QTimer>
#include "canopen.h"
namespace Ui {
class YeYaGangControl;
}
#define NOZERO  999999999
class YeYaGangControl : public QWidget
{
    Q_OBJECT

public:
    explicit YeYaGangControl(QWidget *parent = nullptr);
    explicit YeYaGangControl(uint8_t id, int32_t max =0, int32_t min = 0,int32_t zero=NOZERO, QWidget *parent = nullptr);
    ~YeYaGangControl();
#define readAddr 24575

    enum{

        ENABLE_ADDR = 0x5f7e,
        SPEED_ADDR =0xbff7,
        COMMAND_ADDR = 0xA407,
        SET_POS_ADDR = 0xBFBF,
        STATUS_ADDR = 0xA406,
        REAL_POS_ADDR = 0xBA61,
        REALPOS_H_ADDR = 0xA59E,
        REALPOS_L_ADDR = 0xA59F,
    };

    void setName(QString name);
    QString getName(){return m_name;}
    uint8_t getId(){return m_id;}
    uint8_t m_id;
    QString m_name;
    int32_t max_pos;
    int32_t min_pos;
    int32_t zeroOffect;
    int32_t m_curPos;
    int32_t m_targetPos;
    int16_t m_speed =0;
    int16_t read_speed;
    uint16_t temp_CurPos_H;
    uint16_t temp_CurPos_L;
    uint8_t f_ask_Pos_h =0 ;
    uint8_t f_ask_Pos_l =0 ;
    int16_t max_kaikou;
    int16_t min_kaikou;
    int32_t temp_controlPos;
    uint32_t mesId = 0;
    static bool isRun;
    void updateSendPos(int32_t pos);
    void updateMes(uint32_t mesio);
    void setSpeed(int16_t sp){m_speed = sp; if(m_speed<1) m_speed =1;}
    void enableYeYa(uint16_t enable=0xa000);
    void setTagertPos(int32_t pos);
    void readCurPos();
    void readSpeed();
    void readKaiKou();
    void reciveMessage(struct can_frame fam);
    void updateCurPos(int32_t pos);
    void updateSetPos(int32_t pos);
    void setZeroPos(int32_t pos);
    void setSpeed_1(int16_t speed);
    QTimer controlTakeTimer;
    void controlTask();
    int posChange =0;
    enum{
      FIRST_GETPOS,
      SEND_POS,
      SEND_POS_OVER
    };
    int32_t control_step = FIRST_GETPOS;
private:
    Ui::YeYaGangControl *ui;
signals:
    void sendMessage(struct can_frame fanm);
private slots:
    void on_btnEnable_clicked();
    void on_btnAskPos_clicked();
    void on_btnSetPos_clicked();
    void on_btnSetSpeed_clicked();
    void on_btnAskPos_3_clicked();
    void on_btn_stop_clicked();
};

#endif // YEYAGANGCONTROL_H
